﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WuxiWTO.Mapping;
using System.Data.Linq;

namespace WutiWTO.Common
{
    public class CategoryInfo
    {

        public static string GetTopNav()
        {
            WTODataContext ctx = new WTODataContext();
            var list = (from c in ctx.categories orderby c.sort_num ascending where c.art_parent_kind == 0 && c.visiable == true select c).ToList();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < list.Count; i++)
            {
                if (i == list.Count - 1)
                {
                    sb.Append("<li class='last'>");
                }
                else
                {
                    sb.Append("<li>");
                }
                //如果URL不为空，则跳转到相应的URL
                if (!string.IsNullOrEmpty(list[i].url))
                {
                    sb.Append("<a href=\"");
                    sb.Append(list[i].url);
                }
                else
                {
                    if (list[i].is_special_to_child.HasValue)
                    {
                        //如果设置为跳转到二级分类
                        if (list[i].is_special_to_child == 'Y')
                        {
                            //选取第一个URL为空的二级分类的地址
                            category cat = ctx.categories.FirstOrDefault(p => p.art_parent_kind == list[i].art_kind_id && (p.url == "" || p.url == null));
                            if (cat != null)
                            {
                                sb.AppendFormat("<a href=\"articleList.aspx?pid={0}&cid={1}", list[i].art_kind_id, cat.art_kind_id);
                            }
                            else
                            {
                                //如果二级分类没有取到，还是保留原来的父级分类的地址
                                sb.Append("<a href=\"articleList.aspx?pid=");
                                sb.Append(list[i].art_kind_id);
                            }
                        }
                        else
                        {
                            sb.Append("<a href=\"articleList.aspx?pid=");
                            sb.Append(list[i].art_kind_id);
                        }
                    }
                    else
                    {
                        sb.Append("<a href=\"articleList.aspx?pid=");
                        sb.Append(list[i].art_kind_id);
                    }
                }
                sb.Append("\">");
                sb.Append(list[i].art_kind_name);
                sb.Append("</a></li>");
            }
            return sb.ToString();
        }

        public static string GetLeftMode()
        {
            WTODataContext ctx = new WTODataContext();
            var list = (from c in ctx.categories orderby c.sort_num ascending where c.position == "L" select c).ToList();
            StringBuilder sb = new StringBuilder();
            //或者分类列表
            int flag = 0;
            string cssName = "";
            foreach (category parent in list)
            {
                if (flag == 0)
                {
                    cssName = " con_left_title1";
                }
                else
                {
                    cssName = "";
                }
                sb.Append("<div class=\"con_left_mod\">");
                sb.AppendFormat("<h2 class=\"con_left_title{0}\"><span class=\"left\">", cssName);
                string categoryName = parent.alias == "" ? parent.art_kind_name : parent.alias;
                sb.Append(categoryName);
                sb.Append("</span><span class=\"right\">");

                List<article> arts = new List<article>();
                //迭代循环获得文章列表
                if (parent.art_parent_kind == 0)
                {
                    sb.Append(string.Format("<a href=\"articleList.aspx?pid={0}\">更多&gt;&gt;</a></span></h2>", parent.art_kind_id));
                    arts = ctx.articles.Where(p => p.parent_category_id == parent.art_kind_id).OrderByDescending(p => p.update_date).Take(6).ToList();
                }
                else
                {
                    sb.Append(string.Format("<a href=\"articleList.aspx?pid={0}&amp;cid={1}\">更多&gt;&gt;</a></span></h2>", parent.art_parent_kind, parent.art_kind_id));
                    arts = ctx.articles.Where(p => p.category_id == parent.art_kind_id).OrderByDescending(p => p.update_date).Take(6).ToList();
                }
                sb.Append("<div class=\"con_left_content\">");
                sb.Append("<ul class=\"con_left_list\">");

                foreach (article art in arts)
                {
                    sb.Append(string.Format("<li><a title='{2}' href=\"articledetails.aspx?aid={0}\">{1}</a></li>", art.article_id, Utility.CutStringByLength(art.article_title, 15), art.article_title));
                }
                sb.Append("</ul>");
                sb.Append("</div></div>");
                flag++;
            }

            return sb.ToString();
        }

        public static string GetRightMiddleMode()
        {
            WTODataContext ctx = new WTODataContext();
            var list = (from c in ctx.categories orderby c.sort_num ascending where c.position == "RM" select c).Take(2).ToList();
            int modeCount = list.Count;
            StringBuilder sb = new StringBuilder();
            bool topLevel = false;
            string retValue = "";

            for (int i = 0; i < modeCount; i++)
            {
                string redirectURL = "";
                //模块title的显示文字
                string displayText = list[i].alias == "" ? list[i].art_kind_name : list[i].alias;
                //链接地址
                if (list[i].art_parent_kind == 0)
                {
                    topLevel = true;
                }
                else
                {
                    topLevel = false;
                }
                if (topLevel)
                {
                    redirectURL = "articleList.aspx?pid=" + list[i].art_kind_id;
                }
                else
                {
                    redirectURL = "articleList.aspx?pid=" + list[i].art_parent_kind + "&cid=" + list[i].art_kind_id;
                }
                //模块的css名称不一样
                string cssName = "";
                if (i == 0)
                {
                    cssName = "left";
                }
                else
                {
                    cssName = "right";
                }
                sb.AppendFormat("<div class=\"home_box {0}\">", cssName);
                sb.AppendFormat("<h2 class=\"home_box_tit\"><span class=\"left\">{0}</span><span class=\"right\"><a href=\"{1}\">更多&gt;&gt;</a></span></h2>", displayText, redirectURL);
                sb.Append("<div class=\"home_box_con\">");
                sb.Append("<ul class=\"home_list1\">");

                List<article> articles = new List<article>();
                if (topLevel)
                {
                    articles = (from c in ctx.articles orderby c.update_date descending where c.parent_category_id == list[i].art_kind_id select c).Take(6).ToList();
                }
                else
                {
                    articles = (from c in ctx.articles orderby c.update_date descending where c.parent_category_id == list[i].art_parent_kind && c.category_id == list[i].art_kind_id select c).Take(6).ToList();
                }
                //文章列表
                foreach (article art in articles)
                {
                    sb.AppendFormat("<li><a href=\"articleDetails.aspx?aid={0}\" title= '{1}'>{2}</a></li>", art.article_id, art.article_title, Utility.CutStringByLength(art.article_title, 25));
                }
                sb.Append("</ul>");
                sb.Append("</div>");
                sb.Append("</div>");
            }
            retValue = sb.ToString();
            return retValue;
        }

        //   <div class="home_box left">
        //  <h2 class="home_box_tit"> <span class="left con_titLeft">外商<span class="fontBig">投资</span>企业协会</span><span class="right"><a href="#">更多&gt;&gt;</a></span> </h2>
        //  <div class="home_box_con">
        //    <div class="companyCon"> <img src="images/companyBg.jpg"/>
        //      <p> <strong>海辉软件</strong><br />
        //        海辉是中国值得信赖的科技服务合作伙伴。公司拥有领先的全球运营能力，通过严格的质量标准…</p>
        //    </div>
        //    <ul class="home_list1">
        //      <li><a href="#">无锡锡山区经济开发区</a></li>
        //      <li><a href="#">恩梯梯数据</a></li>
        //      <li><a href="#">无锡NIIT国际软件外包学院</a></li>
        //      <li><a href="#">无锡职院</a></li>
        //    </ul>
        //  </div>
        //</div>
        /// <summary>
        /// 获取页面右方底部左边的文章模块
        /// </summary>
        /// <returns></returns>
        public static string GetRightBottomLeftMode()
        {
            WTODataContext ctx = new WTODataContext();
            var list = (from c in ctx.categories orderby c.sort_num ascending where c.position == "RBL" select c).Take(2).ToList();
            int modeCount = list.Count;
            StringBuilder sb = new StringBuilder();
            bool topLevel = false;

            for (int i = 0; i < modeCount; i++)
            {
                string redirectURL = "";
                //模块title的显示文字
                string displayText = list[i].alias == "" ? list[i].art_kind_name : list[i].alias;
                //链接地址
                if (list[i].art_parent_kind == 0)
                {
                    topLevel = true;
                }
                else
                {
                    topLevel = false;
                }
                if (topLevel)
                {
                    redirectURL = "articleList.aspx?pid=" + list[i].art_kind_id;
                }
                else
                {
                    redirectURL = "articleList.aspx?pid=" + list[i].art_parent_kind + "&cid=" + list[i].art_kind_id;
                }

                sb.Append("<div class=\"home_box left\">");
                sb.AppendFormat("<h2 class=\"home_box_tit\"><span class=\"left con_titLeft\">{0}</span><span class=\"right\"><a href=\"{1}\">更多&gt;&gt;</a></span> </h2>", displayText, redirectURL);
                sb.Append("<div class=\"home_box_con\">");

                List<article> articles = new List<article>();
                if (topLevel)
                {
                    articles = (from c in ctx.articles orderby c.update_date descending where c.parent_category_id == list[i].art_kind_id select c).Take(5).ToList();
                }
                else
                {
                    articles = (from c in ctx.articles orderby c.update_date descending where c.parent_category_id == list[i].art_parent_kind && c.category_id == list[i].art_kind_id select c).Take(5).ToList();
                }

                int flag = 0;
                StringBuilder sb2 = new StringBuilder();
                sb2.Append("<ul class=\"home_list1\">");
                foreach (article art in articles)
                {
                    if (flag == 0)
                    {
                        sb.Append("<div class=\"companyCon\">");
                        sb.AppendFormat("<img src=\"{0}\"/> <p><a title='{1}' href=\"articleDetails.aspx?aid={2}\"><strong>{3}</strong></a><br />{4}</p>", art.article_title_image, art.article_title,
                            art.article_id, Utility.CutStringByLength(art.article_title, 18), Utility.splitstring(art.article_content, 30, true));
                        sb.Append("</div>");
                    }
                    else
                    {
                        sb2.AppendFormat("<li><a title='{0}' href=\"articleDetails.aspx?aid={1}\">{2}</a></li>", art.article_title, art.article_id, Utility.CutStringByLength(art.article_title, 18));
                    }
                    flag++;
                }
                sb2.Append("</ul>");
                sb.Append(sb2);
                sb.Append("</div></div>");
            }

            return sb.ToString();
        }
        /// <summary>
        /// 获取右方底部右边的Tab的文章
        /// </summary>
        /// <returns></returns>
        public static string GetRightBottomMode()
        {
            WTODataContext ctx = new WTODataContext();
            var list = (from c in ctx.categories orderby c.sort_num ascending where c.position == "RB" select c).Take(2).ToList();
            int modeCount = list.Count;
            StringBuilder sb = new StringBuilder();
            bool topLevel = false;

            string currentSpanClass = "class='Outsourcing1'";
            string otherSpanClass = "class='Outsourcing2'";
            StringBuilder sbSpanTab = new StringBuilder();
            StringBuilder sbTabContent = new StringBuilder();

            sbSpanTab.Append("<h2 class=\"home_box_tit OutsourcingTit\">");
            for (int i = 0; i < modeCount; i++)
            {
                string redirectURL = "";
                //模块title的显示文字
                string displayText = list[i].alias == "" ? list[i].art_kind_name : list[i].alias;
                //链接地址
                if (list[i].art_parent_kind == 0)
                {
                    topLevel = true;
                }
                else
                {
                    topLevel = false;
                }
                if (topLevel)
                {
                    redirectURL = "articleList.aspx?pid=" + list[i].art_kind_id;
                }
                else
                {
                    redirectURL = "articleList.aspx?pid=" + list[i].art_parent_kind + "&cid=" + list[i].art_kind_id;
                }
                //sb.Append("<div class=\"home_box right\">");

                //sbSpanTab.AppendFormat("<span {0}>{1}</span><span class=\"right\"><a href=\"{2}\">更多&gt;&gt;</a></span>", currentSpanClass, displayText, redirectURL);
                if (i == 0)
                {
                    sbSpanTab.AppendFormat("<span {0}>{1}</span><a  class=\"right {2}\" href=\"{3}\">更多&gt;&gt;</a>", currentSpanClass, displayText, i == 0 ? "ct" : "ot", redirectURL);
                }
                else
                {
                    sbSpanTab.AppendFormat("<span {0}>{1}</span><a  class=\"right {2}\" href=\"{3}\">更多&gt;&gt;</a>", otherSpanClass, displayText, i == 0 ? "ct" : "ot", redirectURL);
                }


            }
            sbSpanTab.Append(" </h2>");
            for (int i = 0; i < modeCount; i++)
            {
                if (list[i].art_parent_kind == 0)
                {
                    topLevel = true;
                }
                else
                {
                    topLevel = false;
                }

                sbTabContent.AppendFormat("<div class=\"home_box_con {0}\">", i == 0 ? "ct" : "ot");
                List<article> articles = new List<article>();
                if (topLevel)
                {
                    articles = (from c in ctx.articles orderby c.update_date descending where c.parent_category_id == list[i].art_kind_id select c).Take(5).ToList();
                }
                else
                {
                    articles = (from c in ctx.articles orderby c.update_date descending where c.parent_category_id == list[i].art_parent_kind && c.category_id == list[i].art_kind_id select c).Take(5).ToList();
                }
                int flag = 0;
                StringBuilder sb2 = new StringBuilder();
                sb2.Append("<ul class=\"home_list1\">");
                foreach (article art in articles)
                {
                    if (flag == 0)
                    {
                        sbTabContent.Append("<div class=\"companyCon\">");
                        sbTabContent.AppendFormat("<p><img src=\"{0}\"/> <a title='{1}' href=\"articleDetails.aspx?aid={2}\"><strong>{3}</strong></a><br />{4}</p>", art.article_title_image, art.article_title,
                            art.article_id, Utility.CutStringByLength(art.article_title, 18), Utility.splitstring(art.article_content, 30, true));
                        sbTabContent.Append("</div>");
                    }
                    else
                    {
                        sb2.AppendFormat("<li><a title='{0}' href=\"articleDetails.aspx?aid={1}\">{2}</a></li>", art.article_title, art.article_id, Utility.CutStringByLength(art.article_title, 18));
                    }
                    flag++;
                }
                sb2.Append("</ul>");
                sbTabContent.Append(sb2);
                sbTabContent.Append("</div>");
            }
            sb.Append(sbSpanTab);
            sb.Append(sbTabContent);
            return sb.ToString();
        }

        public static string GetBottomNavi()
        {
            WTODataContext ctx = new WTODataContext();
            var list = (from c in ctx.categories orderby c.sort_num ascending where c.position == "B" select c).ToList();
            int modeCount = list.Count;
            StringBuilder sb = new StringBuilder();
            bool topLevel = false;
            for (int i = 0; i < modeCount; i++)
            {
                string redirectURL = "";
                //模块title的显示文字
                string displayText = list[i].alias == "" ? list[i].art_kind_name : list[i].alias;
                //链接地址
                if (list[i].art_parent_kind == 0)
                {
                    topLevel = true;
                }
                else
                {
                    topLevel = false;
                }
                if (topLevel)
                {
                    redirectURL = "articleList.aspx?pid=" + list[i].art_kind_id;
                }
                else
                {
                    redirectURL = "articleList.aspx?pid=" + list[i].art_parent_kind + "&cid=" + list[i].art_kind_id;
                }

                sb.Append("&nbsp;&nbsp;|&nbsp;&nbsp;");
                sb.AppendFormat("<a href=\"{0}\">{1}</a>", redirectURL, displayText);
            }
            return sb.ToString();
        }
        /// <summary>
        /// 获取上一篇的文章
        /// </summary>
        /// <param name="currentID">当前的文章ID</param>
        /// <returns></returns>
        public static usp_get_prev_articleResult GetPrevArticle(int currentID)
        {
            WTODataContext ctx = new WTODataContext();
            var data = from c in ctx.usp_get_prev_article(currentID) select c;
            usp_get_prev_articleResult prev = data.FirstOrDefault();
            return prev;
        }
        /// <summary>
        /// 获取下一篇的文章
        /// </summary>
        /// <param name="currentID">当前文章ID</param>
        /// <returns></returns>
        public static usp_get_next_articleResult GetNextArticle(int currentID)
        {
            WTODataContext ctx = new WTODataContext();
            var data = from c in ctx.usp_get_next_article(currentID) select c;
            usp_get_next_articleResult prev = data.FirstOrDefault();
            return prev;
        }
    }
}
